AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")
include("shared.lua")

function ENT:TestAngles()

	//the default angles are shown first
	local effectdata = EffectData()
	effectdata:SetOrigin(self:GetPos() + (self:GetForward()*60) )
	effectdata:SetAngle(Angle(220,0,0)) //angle is actually a color being sent. P,Y,R = R,G,B
	util.Effect("angle_test", effectdata)
	local effectdata = EffectData()
	effectdata:SetOrigin(self:GetPos() + (self:GetRight()*60) )
	effectdata:SetAngle(Angle(0,220,0))
	util.Effect("angle_test", effectdata)
	local effectdata = EffectData()
	effectdata:SetOrigin(self:GetPos() + (self:GetUp()*60) )
	effectdata:SetAngle(Angle(0,0,220))
	util.Effect("angle_test", effectdata)
	
	//show us the modified angles now.
	local effectdata = EffectData()
	effectdata:SetOrigin(self:GetPos() + ((self:GetTrueForward() *60) +Vector(0,0,10)))
	effectdata:SetAngle(Angle(220,0,0)) //angle is actually a color being sent. P,Y,R = R,G,B
	util.Effect("angle_test", effectdata)
	local effectdata = EffectData()
	effectdata:SetOrigin(self:GetPos() + ((self:GetTrueRight()*60) +Vector(0,0,10)))
	effectdata:SetAngle(Angle(0,220,0))
	util.Effect("angle_test", effectdata)
	local effectdata = EffectData()
	effectdata:SetOrigin(self:GetPos() + ((self:GetTrueUp()*60) +Vector(0,0,10)))
	effectdata:SetAngle(Angle(0,0,220))
	util.Effect("angle_test", effectdata)
	
end

local v15up = Vector(0, 0, 15)
local v20up = Vector(0, 0, 20)
function ENT:PhysicsSimulate(phys, frametime)

	self:TestAngles()
	phys:Wake()
	
	if self.Mayday then
		phys:ApplyForceCenter((self.BaseSpeed / 1.7) * phys:GetMass() * (self:GetTrueUp()*-2))
		phys:AddAngleVelocity(Angle( self.TurnSpeed/2 * frametime * 66 , 0, 0))
		local effectdata = EffectData()
		effectdata:SetOrigin(self:GetPos())
		util.Effect("smoke_trail_big", effectdata)
	end
	
	if self.Crash then
		if self:GetOwner() then
			if self.Mayday then
				if self.Mayday:IsPlayer() then
					self:GetOwner():DestroyVehicle(self.Mayday)
				else
					self:GetOwner():DestroyVehicle()
				end
			else
				self:GetOwner():DestroyVehicle()
			end
		end
	end

	local v = self.Driver
	if v == nil or v == NULL then phys:EnableGravity(true) return SIM_NOTHING end
	if not v:IsValid() then phys:EnableGravity(true) return SIM_NOTHING end
	
	v:SetPos(self:GetPos() + v15up)

	if self.Launch > CurTime() then
		phys:ApplyForceCenter((self.BaseSpeed / 2) * phys:GetMass() * self:GetTrueUp() + v15up)
		return
	elseif self.TakeOff then
		phys:ApplyForceCenter((self.BaseSpeed / 1.7) * phys:GetMass() * self:GetTrueForward())
	end

	if v:KeyDown( IN_JUMP ) then
		if self.TakeOff and self.Launch < CurTime() then
			self:EndSoundLoop()
			self.Land = true
			self.TakeOff = false
			phys:EnableGravity(true)
			self.LandTime = CurTime() + 3
			self:EmitSound(Sound("vehicles/airboat/fan_motor_shut_off1.wav"), 100, math.random(90, 100))
		elseif self.Land and self.LandTime < CurTime() then
			phys:EnableGravity(false)
			self:SetSoundLoop(self.DriveSounds.Idle)
			self.TakeOff = true
			self.Land = false
			self.Launch = CurTime() + .5
			self:EmitSound(Sound("vehicles/airboat/fan_motor_start1.wav"), 100, math.random(90, 100))
		end
	end

	if self.Launch < CurTime() then
		if v:KeyDown( IN_FORWARD ) then
			if v.Inverted then
				phys:AddAngleVelocity(Angle( self.TurnSpeed * frametime * -66 , 0, 0))
			else
				phys:AddAngleVelocity(Angle( self.TurnSpeed * frametime * 66 , 0, 0))
			end 
		elseif v:KeyDown( IN_BACK ) then
			if v.Inverted then
				phys:AddAngleVelocity(Angle( self.TurnSpeed * frametime * 66 , 0, 0))
			else
				phys:AddAngleVelocity(Angle( self.TurnSpeed * frametime * -66 , 0, 0))
			end 
		elseif v:KeyDown( IN_SPEED ) then
			if not self.Land then
				local forward = phys:GetMass() * self.BaseSpeed * self:GetTrueForward()
				phys:ApplyForceCenter(forward) 
				self:SetSoundLoop(self.DriveSounds.Drive)
			end
		elseif v:KeyDown( IN_MOVELEFT ) then
			if not self.Land then
				phys:AddAngleVelocity(Angle( 0,self.TurnSpeed * frametime * 66 , 0)) //was negative
			end
		elseif v:KeyDown( IN_MOVERIGHT ) then
			if not self.Land then
				phys:AddAngleVelocity(Angle( 0,self.TurnSpeed * frametime * -66 , 0))
			end
		end
		if v:KeyDown( IN_DUCK ) then
			phys:ApplyForceCenter((self.BaseSpeed / 1.5) * phys:GetMass() * (self:GetTrueUp()*-2))
			self:SetSoundLoop(self.DriveSounds.Idle)
		end
	end
end

function ENT:SpawnWeapon()
	//weapontbl  ->  name, offset, mount
	local ent = ents.Create(self.WeaponTbl[1])
	if self.WeaponTbl[3] == "frontal" then
		ent:SetPos(self:GetPos() + self:GetTrueForward()*25)
	else
		ent:SetPos(self:GetPos())
	end
	ent:Setup(self.WeaponTbl[2])
	ent:SetPlayer(self:GetOwner())
	ent:SetAngles(self.WeaponTbl[2]+self:GetOwner():EyeAngles())
	ent:Activate()
	ent:Spawn()
	if self.WeaponTbl[3] == "frontal" then
		constraint.Ballsocket( ent, self, 1, 1, ent:GetPos(), 0, 500, 1 )
	else
		ent:SetParent(self)
	end
	self.Weapon = ent
end